package com.eight.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.feign.UploadFeign;
import com.eight.pojo.SysAdmin;
import com.eight.service.AdminService;
import com.eight.util.MenuLeveUtil;
import com.eight.util.WebUtil;
import com.eight.vo.ChartVo;
import com.eight.vo.ComResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: apiController的模板接口，后续编写请按下面规格编写
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-24  08:32
 * @Version: 1.0
 */
@RestController
@RequestMapping("/system/api/admin")
public class AdminApi {

    @Resource(name = "adminService")
    private AdminService adminService;
    @Autowired
    private UploadFeign uploadFeign;


    /**
     * 根据用户的名字查询出来用户的全部信息
     *
     * @param username 用户的登录账号
     * @return 查询的结果
     */
    @GetMapping("/getAdminByName/{username}")
    public ComResult getAdminByName(@PathVariable String username) {
        return new ComResult(2000, "查询成功", adminService.getOne(new QueryWrapper<SysAdmin>().eq("admin_username", username)));
    }

    /**
     * 模糊查询用户
     *
     * @param page  页
     * @param size  条
     * @param admin 条件
     * @return 返回用户数据
     */
    @PostMapping("/getAllAdmin/{page}/{size}")
    public ComResult getAllAdmin(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody SysAdmin admin) {
        return new ComResult(2000, "查询所有用户", adminService.selectSysAdminList(page, size, admin));
    }

    /**
     * 根据id修改用户信息
     *
     * @param admin 所需修改的条件
     * @return 修改成功返回1，否则返回2
     */
    @PostMapping("/updateAdmin")
    public ComResult updateAdmin(@RequestBody SysAdmin admin) {
        return new ComResult(2000, "修改成功", adminService.updateById(admin));
    }

    /**
     * 根据用户id删除用户
     *
     * @param id 目标用户
     * @return 返回删除的结果
     */
    @GetMapping("/deleteAdminById/{id}")
    public ComResult deleteAdminById(@PathVariable("id") Integer id) {
        return new ComResult(2000, "删除成功", adminService.removeById(id));
    }

    /**
     * 增加用户
     *
     * @param admin 要增加的用户
     * @return 返回增加的结果
     */
    @PostMapping("addAdmin")
    public ComResult addAdmin(@RequestBody SysAdmin admin) {
        admin.setRoleId(22);
        admin.setAdminPassword("$10$8SzaxpYYljvqISACz8cwBOfLTlYqR6QqeNwACp6Au.y/Mo8MFKwpa");
        admin.setAdminId(MenuLeveUtil.getRandomString(6));
        admin.setIsDeleted(0);
        admin.setGmtCreate(new Date());
        System.out.println(admin);
        return new ComResult(2000, "添加成功", adminService.addSysAdmin(admin));
    }

    /**
     * 头像上传
     * @param file 文件二进制码 由网页进行传参 不必担心
     * @param dirPath 图片名称
     * @return 返回经过上传到阿里云仓库的切加密过的图片名称
     */
    @RequestMapping(value = "imgUpload/{dirPath}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ComResult imgUpload(@RequestPart(value = "file") MultipartFile file, @PathVariable String dirPath) {
        return uploadFeign.upload(file, dirPath);
    }

    /**
     * 查询所有的用户的人数
     * @return 返回人数
     */
    @PostMapping("/selectAllAdminNum")
    public ComResult selectAllAdminNum() {
        return new ComResult(2000, "查询成功", adminService.selectAllAdminNum());
    }

    /**
     * 查询所有用户的性别
     * @return 返回用户的性别
     */
    @RequestMapping("selectAdminSex")
    public List<ChartVo> selectAdminSex(){
        return adminService.selectAdminSex();
    }

    /**
     * 查询当前登录者的信息
     * @return 返回登录者的信息
     */
    @PostMapping("initAdmin")
    public ComResult initAdmin() {
        return new ComResult(2000, "初始化成功", adminService.initAdmin());
    }

}
